﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

namespace alwitTest.YX
{
    internal class CommNetConstants
    {
        public const int DataFilterPacketNumber = 25;
        public const int ClientPacketSizeMax = 2048;//客户端数据包的最大值
        public const int DataReceiveBufferMaxSize = 60000;
        public const int ClientPacketSize_Remote = 11;
        public const int ClientPacketSize_list = 17;
        public const int DataSendBufferMaxSize = 4000;
        public const int DataSendBufferPacketSize = 1;
        public const int ClientSendResetNumber = 100;
        public const int DefaultAnodeNumber = 64;//48;
        public const int ReceiveBufferCapacity_potcell = 5000;//用于队列
        public const int SolveCyclesMax = 10;
        public const int AnodeDynamicBufferLength = 20;

    }

    internal class PotByteTwoArray_BufferMode
    {
        private byte[,] byteTwoArray = new byte[30, 2];
        private bool[] writeSymbol = new bool[30];
        private int readPointer = 0;
        private int writePointer = 0;
        private int currentPointer = 0;

        private long bufferTotalValue = 0;
        private int bufferTotalCounter = 0;

        [MethodImplAttribute(MethodImplOptions.Synchronized)]
        public void GetByteTwoData(byte[] dataExport)
        {
            for (int i = 0; i < 2; i++)
            {
                dataExport[i] = byteTwoArray[currentPointer, i];
            }

        }

        [MethodImplAttribute(MethodImplOptions.Synchronized)]
        public void GetByteTwoData_BufferMode(byte[] dataExport)
        {
            ReadPointerCheck();

            for (int i = 0; i < 2; i++)
            {
                dataExport[i] = byteTwoArray[readPointer, i];
            }

            writeSymbol[readPointer] = false;
            IncreaseReadPointer();

        }

        [MethodImplAttribute(MethodImplOptions.Synchronized)]
        public void SetByteTwoData(byte[] dataImport, int startIndex)
        {
            int tempData;

            for (int i = 0; i < 2; i++)
            {
                if ((i + startIndex) < dataImport.Length)
                {
                    byteTwoArray[writePointer, i] = dataImport[i + startIndex];
                }
            }

            tempData = byteTwoArray[writePointer, 0];
            tempData = tempData * 256 + byteTwoArray[writePointer, 1];
            bufferTotalValue = bufferTotalValue + tempData;
            bufferTotalCounter = bufferTotalCounter + 1;

            currentPointer = writePointer;
            writeSymbol[writePointer] = true;
            IncreaseWritePointer();

        }

        [MethodImplAttribute(MethodImplOptions.Synchronized)]
        public float GetAverageDataValue()
        {
            float tempData = 0f;

            if (bufferTotalCounter > 0)
            {
                tempData = (float)bufferTotalValue;
                return (float)(bufferTotalValue / bufferTotalCounter);
            }
            else
            {
                return (float)0;
            }
        }

        [MethodImplAttribute(MethodImplOptions.Synchronized)]
        public void ZeroDataValue()
        {
            bufferTotalValue = 0;
            bufferTotalCounter = 0;
        }

        private void ReadPointerCheck()
        {
            if (writeSymbol[readPointer] == false)
            {
                readPointer = currentPointer;

                //if (readPointer < 1)
                //{
                //    readPointer = PotLineDataConstants.BaseDataBufferSize -1;
                //}
                //else
                //{
                //    readPointer = readPointer - 1;
                //}
            }

        }

        private void IncreaseReadPointer()
        {
            if (readPointer > (30 - 2))
            {
                readPointer = 0;
            }
            else
            {
                readPointer = readPointer + 1;
            }
        }

        private void IncreaseWritePointer()
        {
            if (writePointer > (30 - 2))
            {
                writePointer = 0;
            }
            else
            {
                writePointer = writePointer + 1;
            }
        }

    }

    internal class ClientDataSend
    {
        private byte[] m_dataBuffer = new byte[CommNetConstants.ClientPacketSize_Remote];
        private bool m_dataSendFlag;
        private Object m_DataSendCriSection = new object();

        public byte[] GetDataBuffer()
        {
            lock (m_DataSendCriSection)
            {
                return m_dataBuffer;
            }
        }

        public void SetDataBuffer(byte[] dataBuffer)
        {
            lock (m_DataSendCriSection)
            {
                for (int i = 0; i <= dataBuffer.GetUpperBound(0); i++)
                {
                    m_dataBuffer[i] = dataBuffer[i];
                }
            }
        }

        public bool GetDataSendFlag()
        {
            lock (m_DataSendCriSection)
            {
                return m_dataSendFlag;
            }
        }

        public void SetDataSendFlag(bool sendFlag)
        {
            lock (m_DataSendCriSection)
            {
                m_dataSendFlag = sendFlag;
            }
        }

    }

    internal sealed class SocketAsyncClient : IDisposable
    {
        private MainWindow? m_mainWindow = null;

        private object[] m_pList = new object[2];
        private EventHandler m_mainFormUpdate;
        private string m_testValue = "";

        private int m_heartDelayCounter;
        private int m_heartPacketCounter = 0;
        private object m_heartPacketCriSection = new object();

        private CommStreamData m_commStreamData;
        private PotByteTwoArray_BufferMode feedToNowDataBuffer = new PotByteTwoArray_BufferMode();
        private SendQueueBufferPacket m_sendQueueRemote;
        private ResponseBufferPacket m_responseBufferPacket;

        private byte[] m_clientConnect = new byte[6];

        private System.Timers.Timer tmrDataSend = new System.Timers.Timer();
        private int tmrDataSendIsRunning = 0;

        private System.Timers.Timer tmrCommJudgement = new System.Timers.Timer();
        private int tmrCommJudgementIsRunning = 0;

        private SocketAsyncEventArgs m_dataSendArgs = new SocketAsyncEventArgs();
        private SocketAsyncEventArgs m_dataReceiveArgs = new SocketAsyncEventArgs();
        private SocketAsyncEventArgs m_connectArgs = new SocketAsyncEventArgs();
        private Socket clientMonitor;
        private IPEndPoint iep;
        private string m_comIP;
        private int m_dataReceiveLength;
        private int m_dataReceiveOffset = 0;
        private bool m_dataSendFlag = false;

        private int m_dataSendCountIndex = 0;

        private int m_netPacketAmount;
        private object m_criNetPacketAmount = new object();
        public int GetNetPacketAmount()
        {
            lock (this.m_criNetPacketAmount)
            {
                return this.m_netPacketAmount;
            }
        }
        public void SetNetPacektAmount(int dataValue)
        {
            lock (this.m_criNetPacketAmount)
            {
                this.m_netPacketAmount = dataValue;
            }
        }
        private void AddNetPacketAmount(int dataValue)
        {
            lock (this.m_criNetPacketAmount)
            {
                this.m_netPacketAmount = this.m_netPacketAmount + dataValue;
            }
        }

        private int m_dynamicCountIndex = 0;//动态曲线循环计数用
        private int m_dynamicCountIndexOrigin = 0;
        private Object m_DynamicCurveCriSection = new object();
        private Object m_DynamicCurveCriSectionOrigin = new object();

        public int GetDynamicCountIndex()
        {
            lock (m_DynamicCurveCriSection)
            {
                return m_dynamicCountIndex;
            }
        }
        public void SetDynamicCountIndex(int dynamicCountIndex)
        {
            lock (m_DynamicCurveCriSection)
            {
                m_dynamicCountIndex = dynamicCountIndex;
            }
        }

        public int GetDynamicCountIndexOrigin()
        {
            lock (m_DynamicCurveCriSectionOrigin)
            {
                return m_dynamicCountIndexOrigin;
            }
        }
        public void SetDynamicCountIndexOrigin(int dynamicCountIndex)
        {
            lock (m_DynamicCurveCriSectionOrigin)
            {
                m_dynamicCountIndexOrigin = dynamicCountIndex;
            }
        }

        private byte[] m_dataIOBuffer = new byte[CommNetConstants.ClientPacketSizeMax];
        private byte[] m_dataBuffer = new byte[CommNetConstants.ClientPacketSizeMax];
        private byte[] m_dataSendBuffer = new byte[CommNetConstants.ClientPacketSize_Remote];

        private byte[] m_dataReceiveBuffer = new byte[CommNetConstants.DataReceiveBufferMaxSize];
        private long m_dataBufferPointerOrigin = 0;
        private long m_dataBufferPointerCurrent;
        private Object m_BufferCriSection = new object();
        public long GetPointerCurrent()
        {
            lock (m_BufferCriSection)
            {
                return m_dataBufferPointerCurrent;
            }
        }

        private void SetPointerCurrent(long dataPointer)
        {
            lock (m_BufferCriSection)
            {
                m_dataBufferPointerCurrent = dataPointer;
            }
        }

        public long GetPointerOrigin()
        {
            return m_dataBufferPointerOrigin;
        }

        public void SetPointerOrigin(long dataPointer)
        {
            m_dataBufferPointerOrigin = dataPointer;
        }

        public byte[] GetReceiveBuffer()
        {
            return m_dataReceiveBuffer;
        }

        private ClientDataSend[] clientDataSend = new ClientDataSend[CommNetConstants.DataSendBufferMaxSize];
        private long m_dataSendPointerOrigin;
        private long m_dataSendPointerCurrent;
        private int m_packetNumber = 0;
        private Object m_ClientCriSection = new object();

        public long GetSendPointerCurrent()
        {
            lock (m_ClientCriSection)
            {
                return m_dataSendPointerCurrent;
            }
        }

        private void SetSendPointerCurrent(long dataPointer)
        {
            lock (m_ClientCriSection)
            {
                m_dataSendPointerCurrent = dataPointer;
            }
        }

        public void DataSendPush(byte[] dataBuffer)
        {
            long jk;

            jk = GetSendPointerCurrent();

            clientDataSend[jk].SetDataBuffer(dataBuffer);
            clientDataSend[jk].SetDataSendFlag(true);

            jk = jk + 1;
            if (jk >= CommNetConstants.DataSendBufferMaxSize)
            {
                jk = 0;
            }

            SetSendPointerCurrent(jk);

            if (this.tmrDataSend.Enabled == false)
            {
                this.tmrDataSend.Enabled = true;
            }

        }

        private int DataSendPop(ResponseBufferPacket responseBufferPacket)
        {
            if (HeartPacketCounter > 20)
            {
                responseBufferPacket.PacketBuffer[0] = 240;
                responseBufferPacket.PacketBuffer[1] = (byte)(DateTime.Now.Year / 256);
                responseBufferPacket.PacketBuffer[2] = (byte)(DateTime.Now.Year % 256);
                responseBufferPacket.PacketBuffer[3] = (byte)(DateTime.Now.Month);
                responseBufferPacket.PacketBuffer[4] = (byte)(DateTime.Now.Day);
                responseBufferPacket.PacketBuffer[5] = (byte)(DateTime.Now.Hour);
                responseBufferPacket.PacketBuffer[6] = (byte)(DateTime.Now.Minute);
                responseBufferPacket.PacketBuffer[7] = (byte)(DateTime.Now.Second);
                responseBufferPacket.PacketBuffer[8] = 0;
                responseBufferPacket.PacketBuffer[9] = 0;
                responseBufferPacket.PacketBuffer[10] = 0;
                responseBufferPacket.PacketLength = 11;

                //--------------------//
                HeartPacketCounter = 0;

                return 1;

            }
            else
            {
                IncreaseHeartCounter();
            }

            //----------------------------//
            if (this.m_sendQueueRemote.PopPacket(responseBufferPacket) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        public SocketAsyncClient(string serverIP, int serverPort, CommStreamData commStreamData, MainWindow mainWindow)
        {
            this.m_commStreamData = commStreamData;
            this.m_mainWindow = mainWindow;

            m_clientConnect[0] = 165;
            m_clientConnect[1] = 90;
            m_clientConnect[2] = 0;
            m_clientConnect[3] = 1;
            m_clientConnect[4] = 165;
            m_clientConnect[5] = 90;

            for (int i = 0; i < clientDataSend.Length; i++)
            {
                clientDataSend[i] = new ClientDataSend();
            }

            this.m_sendQueueRemote = new SendQueueBufferPacket(100, 11);
            this.m_responseBufferPacket = new ResponseBufferPacket(11);

            //Connecting...
            iep = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
            clientMonitor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.m_comIP = serverIP;

            m_dataSendArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnSend);
            m_dataReceiveArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);

            m_connectArgs.RemoteEndPoint = iep;
            m_connectArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);

            m_dataReceiveArgs.SetBuffer(m_dataIOBuffer, 0, m_dataIOBuffer.Length);

            //Timer
            this.tmrDataSend.Interval = 100;
            this.tmrDataSend.AutoReset = true;
            this.tmrDataSend.Elapsed += new System.Timers.ElapsedEventHandler(tmrDataSend_Elapsed);

            this.tmrCommJudgement.Interval = 30000;
            this.tmrCommJudgement.AutoReset = true;
            this.tmrCommJudgement.Elapsed += new System.Timers.ElapsedEventHandler(tmrCommJudgement_Elapsed);

            m_mainFormUpdate = new System.EventHandler(UpdateMainFormUI);

        }

        private async void tmrDataSend_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            int dataSize = 0;

            try
            {
                if (Interlocked.Exchange(ref this.tmrDataSendIsRunning, 1) == 0)
                {
                    if (clientMonitor != null)
                    {
                        if (m_dataSendFlag == false && clientMonitor.Connected == true)
                        {
                            m_dataSendCountIndex = 0;

                            dataSize = DataSendPop(m_responseBufferPacket);
                            if (dataSize > 0)
                            {
                                await Task.Run(() =>
                                {
                                    m_dataSendFlag = true;
                                    //  准备发送/接收操作的参数。      
                                    //m_dataSendArgs.SetBuffer(m_responseBufferPacket.PacketBuffer, 0, m_responseBufferPacket.PacketLength);
                                    //clientMonitor.SendAsync(m_dataSendArgs);
                                    clientMonitor.Send(m_responseBufferPacket.PacketBuffer, 0, m_responseBufferPacket.PacketLength, System.Net.Sockets.SocketFlags.None);
                                    m_dataSendFlag = false;
                                });
                            }

                        }
                        else
                        {
                            if (m_dataSendCountIndex > CommNetConstants.ClientSendResetNumber)
                            {
                                ClientSocketReset();
                            }
                            else
                            {
                                m_dataSendCountIndex = m_dataSendCountIndex + 1;
                            }
                        }
                    }

                    Interlocked.Exchange(ref this.tmrDataSendIsRunning, 0);

                }
            }
            //catch
            //{
            //    //Interlocked.Exchange(ref this.tmrDataSendIsRunning, 0);
            //}
            catch (Exception ex)
            {
                MessageBox.Show(this.m_mainWindow, ex.ToString().Trim(), "警告", MessageBox.MessageBoxButtons.Ok);
            }
            finally
            {
            }
        }

        private void tmrCommJudgement_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (Interlocked.Exchange(ref this.tmrCommJudgementIsRunning, 1) == 0)
                {
                    if (this.m_commStreamData.CommCheckSymbol == false)
                    {
                        this.m_commStreamData.CommCheckSymbol = true;
                    }
                    else
                    {
                        this.m_commStreamData.CommCheckSymbol = false;
                        ClientSocketReset();

                        //this.m_testValue = "数据套节字复位";
                        //m_mainForm.BeginInvoke(m_mainFormUpdate, m_pList);

                    }

                    Interlocked.Exchange(ref this.tmrCommJudgementIsRunning, 0);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this.m_mainWindow, ex.ToString().Trim(), "警告", MessageBox.MessageBoxButtons.Ok);
            }
            finally
            {
            }

        }

        public void ClientStart()
        {
            this.tmrDataSend.Enabled = true;
            this.tmrCommJudgement.Enabled = true;

            clientMonitor.ConnectAsync(m_connectArgs);

        }

        // 释放SocketClient实例。
        public void Dispose()
        {
            tmrDataSend.Enabled = false;
            tmrCommJudgement.Enabled = false;

            if (clientMonitor != null)
            {
                if (clientMonitor.Connected)
                {
                    //  关闭与客户端关联的Socket   
                    try
                    {
                        clientMonitor.Shutdown(SocketShutdown.Both);
                        //System.Threading.Thread.Sleep(2000);

                    }
                    catch (Exception)
                    {
                        //  如果客户端处理已经关闭，抛出异常         
                    }
                }

                clientMonitor.Close();
                clientMonitor = null;
            }

            GC.SuppressFinalize(this);

        }

        private void OnConnect(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                if (clientMonitor != null)
                {
                    clientMonitor.Send(m_clientConnect);

                    //  准备接收。  
                    clientMonitor.ReceiveAsync(m_dataReceiveArgs);
                }
            }
            catch
            {
            }
        }

        private void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            bool asyncReceive = false;

            try
            {
                if (e.SocketError == SocketError.Success)
                {
                    if (e.LastOperation == SocketAsyncOperation.Receive)
                    {
                        if (clientMonitor != null)
                        {
                            if (clientMonitor.Connected)
                            {
                                m_dataReceiveLength = e.BytesTransferred;
                                AddNetPacketAmount(m_dataReceiveLength);

                                if (m_dataReceiveLength > 0)
                                {
                                    m_dataReceiveOffset = e.Offset;

                                    if (m_dataReceiveLength <= CommNetConstants.ClientPacketSizeMax)
                                    {
                                        Buffer.BlockCopy(e.Buffer, m_dataReceiveOffset, m_dataBuffer, 0, m_dataReceiveLength);
                                    }
                                    else
                                    {
                                        m_dataBuffer = new byte[m_dataReceiveLength];
                                        Buffer.BlockCopy(e.Buffer, m_dataReceiveOffset, m_dataBuffer, 0, m_dataBuffer.Length);
                                    }

                                    if (MainWindow.m_packetTypeMode == false)
                                    {
                                        ReceiveDataFromPotcell(e.Buffer, m_dataReceiveOffset, m_dataReceiveLength);
                                        this.m_commStreamData.CommCheckSymbol = false;
                                    }
                                    else
                                    {
                                        SampleNetboxBufferData(m_dataBuffer, m_dataReceiveLength);
                                    }

                                    //SampleDataReceive(m_dataReceiveBuffer, m_dataBuffer, m_dataReceiveLength);

                                    //this.m_testValue =m_dataReceiveLength.ToString() + "---" + (m_dataReceiveLength % ClientConstants.MonitorPacketSize).ToString();
                                    //m_mainForm.BeginInvoke(m_mainFormUpdate, m_pList);


                                    //  准备接收。  
                                    if (clientMonitor != null)
                                    {
                                        //using (ExecutionContext.SuppressFlow()) 
                                        asyncReceive = clientMonitor.ReceiveAsync(e);
                                    }

                                }

                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this.m_mainWindow, ex.ToString().Trim(), "警告", MessageBox.MessageBoxButtons.Ok);
            }
            finally
            {
            }

        }

        private void ReceiveDataFromPotcell(byte[] dataBuffer, int offset, int len)
        {
            if (this.m_commStreamData != null)
            {
                this.m_commStreamData.PushDataIntoListBuffer(dataBuffer, offset, len);
            }
        }

        private void OnSend(object sender, SocketAsyncEventArgs e)
        {
            //long jp;
            //long jk;

            m_dataSendFlag = false;

            //try
            //{
            //    //  发出发送完成信号。       
            //    if (e.SocketError == SocketError.Success)
            //    {
            //        if (e.LastOperation == SocketAsyncOperation.Send)
            //        {
            //            jk = m_dataSendPointerOrigin;

            //            for (int j = 1; j <= m_packetNumber; j++)
            //            {
            //                jp = jk + j - 1;
            //                if (jp > (AnodeConstants.DataSendBufferMaxSize - 1))
            //                {
            //                    jp = jp - AnodeConstants.DataSendBufferMaxSize;
            //                }

            //                clientDataSend[jp].SetDataSendFlag(false);

            //                m_dataSendPointerOrigin = m_dataSendPointerOrigin + 1;
            //                if (m_dataSendPointerOrigin >= AnodeConstants.DataSendBufferMaxSize)
            //                {
            //                    m_dataSendPointerOrigin = 0;
            //                }

            //            }

            //        }
            //    }
            //}
            //catch
            //{
            //}
            //finally
            //{
            //    m_dataSendFlag = false;
            //    m_dataSendCountIndex = 0;
            //}
        }

        private void ClientSocketReset()
        {
            try
            {
                if (clientMonitor.Connected)
                {
                    clientMonitor.Shutdown(SocketShutdown.Both);
                    //System.Threading.Thread.Sleep(2000);

                }
            }
            catch
            {
            }

            try
            {
                clientMonitor.Close();
                //clientMonitor.Dispose();
                clientMonitor = null;

                m_dataSendFlag = false;
                m_dataSendCountIndex = 0;

                System.Threading.Thread.Sleep(2000);
                clientMonitor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientMonitor.ConnectAsync(m_connectArgs);

            }
            catch
            {
            }

        }

        private void SampleNetboxBufferData(byte[] dataBuffer, int dataSize)
        {
            int jk;

            try
            {
                if (dataSize % CommNetConstants.ClientPacketSize_Remote == 0)
                {
                    this.m_commStreamData.CommCheckSymbol = false;

                    jk = dataSize / CommNetConstants.ClientPacketSize_Remote;
                    for (int j = 0; j < jk; j++)
                    {
                        this.m_commStreamData.AnodeQueueReceive.Push(dataBuffer, j * CommNetConstants.ClientPacketSize_Remote, 0, CommNetConstants.ClientPacketSize_Remote, "8");
                    }
                }

            }
            catch
            {
            }
            finally
            {
            }

        }

        private void SampleDataReceive(byte[] dataReceiveBuffer, byte[] dataBuffer, int dataSize)
        {
            long bufferSizeStub;
            long bufferSize;
            long jk;

            try
            {
                jk = GetPointerCurrent();
                bufferSize = dataSize + jk;

                if (bufferSize > CommNetConstants.DataReceiveBufferMaxSize)
                {
                    bufferSizeStub = CommNetConstants.DataReceiveBufferMaxSize - jk;
                    for (int j = 1; j <= bufferSizeStub; j++)
                    {
                        dataReceiveBuffer[jk + j - 1] = dataBuffer[j - 1];
                    }

                    if (bufferSizeStub >= 0)
                    {
                        SetPointerCurrent(dataSize - bufferSizeStub);
                        jk = GetPointerCurrent();

                        for (int j = 0; j < jk; j++)
                        {
                            dataReceiveBuffer[j] = dataBuffer[bufferSizeStub + j];
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < dataSize; j++)
                    {
                        dataReceiveBuffer[jk + j] = dataBuffer[j];
                    }

                    SetPointerCurrent(jk + dataSize);

                }
            }
            catch
            {
            }
            finally
            {
            }

        }

        private void UpdateMainFormUI(object sender, System.EventArgs e)
        {
            //m_mainForm.RefreshMainTestUI(this.m_testValue);
        }

        public bool GenerateHeartPacket(byte[] dataBuffer)
        {
            dataBuffer[0] = 240;
            dataBuffer[1] = (byte)(DateTime.Now.Year / 256);
            dataBuffer[2] = (byte)(DateTime.Now.Year % 256);
            dataBuffer[3] = (byte)(DateTime.Now.Month);
            dataBuffer[4] = (byte)(DateTime.Now.Day);
            dataBuffer[5] = (byte)(DateTime.Now.Hour);
            dataBuffer[6] = (byte)(DateTime.Now.Minute);
            dataBuffer[7] = (byte)(DateTime.Now.Second);
            dataBuffer[8] = 0;
            dataBuffer[9] = 0;
            dataBuffer[10] = 0;

            return true;

        }

        internal string ConnectIP
        {
            get
            {
                return this.m_comIP;
            }
        }

        internal PotByteTwoArray_BufferMode FeedToNowDataBuffer
        {
            get
            {
                return this.feedToNowDataBuffer;
            }
        }

        internal SendQueueBufferPacket SendQueueRemote
        {
            get
            {
                return this.m_sendQueueRemote;
            }
        }

        public int HeartPacketCounter
        {
            get
            {
                lock (this.m_heartPacketCriSection)
                {
                    return this.m_heartPacketCounter;
                }
            }
            set
            {
                lock (this.m_heartPacketCriSection)
                {
                    this.m_heartPacketCounter = value;
                }
            }
        }

        public void IncreaseHeartCounter()
        {
            lock (this.m_heartPacketCriSection)
            {
                this.m_heartPacketCounter = this.m_heartPacketCounter + 1;
            }
        }

    }

}
